<?php

namespace Modules\Goods\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Goods\Entities\Attr;
use Modules\Goods\Entities\Fenlei;
use Modules\Goods\Entities\FenleiAttr;
use Modules\Goods\Entities\Goods;
use Modules\Goods\Http\Requests\GoodsRequest;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
//        dd(\HDModule::getMenuByModule());
        //获取所有的商品数据
        $goods = Goods::paginate(5);
        return view('goods::goods.index',compact('goods'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        //获取所有的顶级分类数据
        $alltopfenlei = Fenlei::where('pid',0)->get();
//        dd($alltopfenlei);
        return view('goods::goods.create',compact('alltopfenlei'));
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
//        $path=[];
//        foreach ($request->file('pisc') as $v){
//            $time=date('Ymd');
//            $path[]=$v->store($time,'pp');
//        }
//        dd($path);
        //获取提交的所有post数据
        $post = $request->all();
//        dd($post);
        $post['cid'] = implode(',',$post['cid']);
        $post['attrs'] = implode(',',$post['attrs']);
        //将post数据存入goods表中
        Goods::create($post);
        //设置闪存，并跳转
        session()->flash('success','商品添加成功');
        return redirect()->route('goods.index');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('goods::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit($id)
    {
        //找到对应id的商品数据
        $goods = Goods::find($id);

        //获取被修改的商品数据所属的顶级分类，二级分类，三级分类等数据
        $ercid = explode(',',$goods['cid'])[1];
//        dd($ercid);
        $sancid = explode(',',$goods['cid'])[2];
        //获取二级分类数据和三级分类数据
        $ercate = Fenlei::find($ercid);
//        dd($ercate);
        $sancate = Fenlei::find($sancid);
        //获取当前商品所属分类的可用属性数据】】
        $keyongAttrs = $this->getAttrs($sancid);
//        dd($keyongAttrs);
        //获取所有的顶级分类
        $alltopfenlei = Fenlei::where('pid',0)->get();
        return view('goods::goods.edit',compact('goods','alltopfenlei','ercate','sancate','keyongAttrs'));
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request,$id)
    {
        //获取需要修改的对应id的商品数据
        $goods = Goods::find($id);

        $goods->gname = $request->input('gname');
        $goods->attrs = implode(',',$request->input('attrs'));
        $goods->price = $request->input('price');
        $goods->miaoshu = $request->input('miaoshu');
        $goods->click = $request->input('click');
        //图片的编辑有问题，还不一定可以删除，就是下面这个代码
        $goods->pics = $request->input('pics');

        $goods->content = $request->input('content');

//        dd($goods->attrs);
        //保存
        $goods->save();
        //设置闪存并跳转
        session()->flash('success','商品编辑成功');
        return redirect()->route('goods.index');

    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
        //调用模块来删除id对应的数据
        Goods::destroy($id);
        session()->flash('success','商品删除成功');
        return redirect()->route('goods.index');
    }

    //找到对应cid的子分类
    public function getSonfenlei($cid){
        $sonfenlei = Fenlei::where('pid',$cid)->get();
        return $sonfenlei;
    }

    //找到cid对应的子分类的对应属性
    public function getAttrs($cid){

        $keyongAttr = FenleiAttr::where('fenlei_id',$cid)->pluck('attr_id');
        $Attrs = Attr::whereIn('id',$keyongAttr)->get();
        return $Attrs;
    }



}
